package elven.codingit.easyutil.common;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 文件类型工具类
 * @author elven 2016年5月24日
 */
public class FileTypeUtils {
	
	private static final Logger log = LoggerFactory.getLogger(FileTypeUtils.class);
	private static final Properties properties = new Properties();
	private static final String[] keys = {"image", "video"};
	
	public static final Integer FILE_TYPE_IMAGE = 0;
	public static final Integer FILE_TYPE_VIDEO = 1;
	
	static{
		try {
			properties.load(FileTypeUtils.class.getResourceAsStream("/filetype.properties"));
			log.info("ready properties... ...");
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
	}
	
	public static String fileType(int type){
		return properties.getProperty(keys[type]);
	}
	
	public static boolean typeCheck(String fileName, int type){
		if(fileType(type) == null){
			return false;
		}
		String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
		String[] types = fileType(type).split(";");
		for(String temp : types){
			if(temp.equalsIgnoreCase(fileType)){
				return true;
			}
		}
		return false;
	}
	public static void main(String[] args) throws Exception {
		String theKeys = "%E5%AE%81%E6%B1%9F";
		System.out.println(URLDecoder.decode(theKeys, "utf-8"));
	}
}